<template>
    <div>
        <el-tree
            :data="RightTreeckeck.length==0?RightRole:RightTreeckeck"
            show-checkbox
            node-key="id"
            @check-change="checkchange"
            :default-expanded-keys="[2, 3]"
            :default-checked-keys="listcheck"
            :check-strictly="true"
            >
        </el-tree>
    </div>
</template>

<script>
import {MenuCheckRole} from '@/api/jurisdiction'

  export default {
    name:'RightRoleTree',
    props:{
      RightRole:{
        type:Array,
        required: true
      },
      LeftSingleId:{
        type:Number,
        required: true
      },
      RightTreeckeck:{
        type:Array,
        required: true
      }
    },
    data() {
      return {
        listcheck:[],  //数组传递过来的值
      }
    },
    methods:{
      checkchange(data,node){
        if(this.RightTreeckeck.length==0){
          alert('请选择菜单谢谢配合')
        }else{
          if(node==true){
            MenuCheckRole({permissionID:this.LeftSingleId,strRoleID:data.RoleID,check:1}).then((res)=>{
              alert(res.Message)
            })
          }else{
            MenuCheckRole({permissionID:this.LeftSingleId,strRoleID:data.RoleID,check:0}).then((res)=>{
              alert(res.Message)
            })
          }
        }
      },
      getTreeList(arr, result) {
        var self = this;
        arr.forEach(function(item) {
      if (item.children) {
      result.push({ RoleID: item.RoleID, ParentID: item.ParentID, label: item.label, IsChecked: item.IsChecked });
      self.getTreeList(item.children, result)
      } else {
      result.push({ RoleID: item.RoleID, ParentID: item.ParentID, label: item.label, IsChecked: item.IsChecked  });
      }
      })
      return result
      }
    },
    watch:{
      RightTreeckeck(val){
        console.log(val)
         let list=this.getTreeList(val,[])
         console.log(list)
         let check=[]
         list.forEach((el)=>{
           if(el.IsChecked==true){
             check.push(el.RoleID)
           }
         })
         this.listcheck=check
      }
    }
  };
</script>